package sk.naive.talker;

import sk.naive.talker.util.Utils;

/**
 * TimerTask.
 *
 * @author <a href="mailto:virgo@naive.deepblue.sk">Richard "Virgo" Richter</a>
 * @version $Revision: 1.2 $ $Date: 2005/10/04 22:35:25 $
 */
public abstract class TimerTask extends java.util.TimerTask {
	private String name;
	private TimerService timer;
	private long count;

	protected TimerTask(String taskName) {
		this.name = taskName;
	}

	public String getName() {
		return name;
	}

	public long getCount() {
		return count;
	}

	public void setTimerService(TimerService timerService) {
		timer = timerService;
	}

	public boolean cancel() {
		boolean retVal = super.cancel();
		timer.remove(this);
		return retVal;
	}

	public final void run() {
		count++;
		try {
			execute();
		} catch (Throwable t) {
			Utils.unexpectedExceptionWarning(t);
		}
	}

	protected abstract void execute();
}
